package com.group5.service;

import com.group5.pojo.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AdminService {
    public PlatformExample selectPlatformByPlatformExample(Platform platform){
        PlatformExample platformExample=new PlatformExample();
        if(platform.getOid()!=null){
            platformExample.or(platformExample.createCriteria().andOidEqualTo(platform.getOid()));
            return platformExample;
        }
        //下面的这些其实都没用,但是写的时候很困难,我先注释掉了,等最后再删
//        platformExample.or(platformExample.createCriteria().
//                andBuyer_stateEqualTo(platform.getBuyer_state()).
//                andOnameLike("%"+platform.getOname()+"%").
//                andOmnemonics_codeLike(platform.getOmnemonics_code()+"%").
//                andOstateEqualTo(platform.getOstate()));
//        platformExample.or(platformExample.createCriteria().
//                andSeller_stateEqualTo(platform.getSeller_state()).
//                andBuyer_stateEqualTo(platform.getBuyer_state()).
//                andOnameLike("%"+platform.getOname()+"%").
//                andOmnemonics_codeLike(platform.getOmnemonics_code()+"%").
//                andOstateEqualTo(platform.getOstate()));
//        platformExample.or(platformExample.createCriteria().
//                andManufacturer_stateEqualTo(platform.getManufacturer_state()).
//                andOnameLike("%"+platform.getOname()+"%").
//                andOmnemonics_codeLike(platform.getOmnemonics_code()+"%").
//                andOstateEqualTo(platform.getOstate()));
        return platformExample;
    }


    //根据oname返回Platform
    public PlatformExample SelectPlatformeByName(String oname){
        PlatformExample platformExample = new PlatformExample();
        platformExample.or(platformExample.createCriteria().andOnameEqualTo(oname));
        return platformExample;
    }


    /**
     * 查找所有drugInfo的Example
     * @return 空的drugInfoExample
     */

    public DrugInfoExample AllDrugsExample(){
        DrugInfoExample drugInfoExample=new DrugInfoExample();
        return drugInfoExample;
    }

    /**
     * 获取当前系统时间，用于修改有关时间的参数，格式：yyyy-MM-dd HH:mm:ss
     * @return 当前系统时间
     */

    public String getTime(){
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    /**
     * 初始化drugInfo前端未提供的信息
     * @param drugInfo
     */

    public void initDrug(DrugInfo drugInfo){
        drugInfo.setCreation_date(getTime());
        drugInfo.setUpdated_date(getTime());
        drugInfo.setEnd_date("2099-12-31 23:59:59");
        drugInfo.setState_of_drug("审核通过");
        drugInfo.setClass_code(-1);
    }

    /**
     * 逻辑删除drugInfo，将状态设为"删除"
     * @param drugInfo
     */

    public void DeleteDrug(DrugInfo drugInfo){
        drugInfo.setUpdated_date(getTime());
        drugInfo.setState_of_drug("删除");
    }

    /**
     * 根据药品名称和生产厂商查找药品
     *
     * @param drugInfo
     * @return 返回带有名称和厂商限定的Example
     */

    public DrugInfoExample DrugNameManufacturerExample(DrugInfo drugInfo){
        DrugInfoExample drugInfoExample=new DrugInfoExample();
        drugInfoExample.or(drugInfoExample.createCriteria().andDrug_nameEqualTo(drugInfo.getDrug_name()).andManufacturerEqualTo(drugInfo.getManufacturer()));
        return drugInfoExample;
    }

    /**
     * 查找所有未审核的drugInfo
     * @return 返回未审核药品的Example
     */

    public DrugInfoExample AllUnauditedDrugExample(){
        DrugInfoExample drugInfoExample=new DrugInfoExample();
        drugInfoExample.or(drugInfoExample.createCriteria().andState_of_drugEqualTo("申请状态"));
        return drugInfoExample;
    }

    /**
     * 根据审核代码设置药品状态：
     *  0：不通过
     *  1：可用
     * @param drugInfo
     * @param check_code
     */

    public void AuditDrugByAuditCode(DrugInfo drugInfo,int check_code){
        if(check_code==1){
            drugInfo.setState_of_drug("审核通过");
        }else{
            drugInfo.setState_of_drug("不通过");
        }
        drugInfo.setUpdated_date(getTime());
    }

    /**
     * 初始化药品树
     *
     * @return 返回所有DrugTag
     */

    public List<DrugTag> initDrugTag(){
        File file=new File("/Users/limingyu/Desktop/data2.txt");//放在了resource中
        int upper_code=0;
        int class_code=0;
        int FirstFloor=1;
        int SecondFloor=1;
        String pattern=null;
        List<DrugTag> drugTagList = new ArrayList<>();
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            String s=null;
            while ((s=br.readLine())!=null){
                if(s.charAt(0)=='第'){
                    pattern="(\\s.*?\\.)";
                    Pattern p=Pattern.compile(pattern);
                    Matcher matcher= p.matcher(s);
                    String class_name = null;
                    if (matcher.find()){
                        class_name=matcher.group(0).substring(1,matcher.group(0).length()-1);
                    }
                    
                    upper_code=0;
                    class_code++;
                    FirstFloor=class_code;
                    DrugTag drugTag=new DrugTag(class_code,class_name,"",upper_code,false);
                    drugTagList.add(drugTag);
                }else if(s.charAt(0)!='('){
                    pattern="(、.*?\\.)";
                    Pattern p=Pattern.compile(pattern);
                    Matcher matcher= p.matcher(s);
                    String class_name = null;
                    if (matcher.find()){
                        class_name=matcher.group(0).substring(1,matcher.group(0).length()-1);
                    }
                    upper_code=FirstFloor;
                    class_code++;
                    SecondFloor=class_code;
                    DrugTag drugTag=new DrugTag(class_code,class_name,"",upper_code,false);
                    drugTagList.add(drugTag);
                }else {
                    pattern="(\\).*?\\.)";
                    Pattern p=Pattern.compile(pattern);
                    Matcher matcher= p.matcher(s);
                    String class_name = null;
                    if (matcher.find()){
                        class_name=matcher.group(0).substring(1,matcher.group(0).length()-1);
                    }
                    upper_code=SecondFloor;
                    class_code++;
                    DrugTag drugTag=new DrugTag(class_code,class_name,"",upper_code,false);
                    drugTagList.add(drugTag);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return drugTagList;
    }

    public PlatformExample selectPlatformByOid(int oid)
    {
        PlatformExample platformExample = new PlatformExample();
        platformExample.or(platformExample.createCriteria().andOidEqualTo(oid));
        return platformExample;
    }


    public PlatformExample AllPlatformExample(){
        PlatformExample platformExample = new PlatformExample();
        return platformExample;
    }

    public PlatformExample selectPlatformExample(Platform platform)
    {
        PlatformExample platformExample = new PlatformExample() ;
        platformExample.or(platformExample.createCriteria().andOidEqualTo(platform.getOid()));
        return platformExample;
    }

    public int PlatformType(Platform platform){
        if(platform.getSeller_state()==2){
            platform.setSeller_state(4);
            return 1;
        }

        else if(platform.getBuyer_state()==2){
            platform.setBuyer_state(4);
            return 2;
        }
        else if(platform.getManufacturer_state()==2){
            platform.setManufacturer_state(4);
            return 3;
        }

        return 0;
    }

    public boolean isPassedPlatform(Platform platform){
        return PlatformType(platform) != 0;
    }

    public UserssExample selectUserByOidExample(int oid){
        UserssExample userssExample=new UserssExample();
        userssExample.or(userssExample.createCriteria().andOidEqualTo(oid));
        return userssExample;
    }

    public PlatformExample AllUnauditedPlatformExample(){
        PlatformExample platformExample=new PlatformExample();
        platformExample.or(platformExample.createCriteria().andSeller_stateEqualTo(2));
        platformExample.or(platformExample.createCriteria().andBuyer_stateEqualTo(2));
        platformExample.or(platformExample.createCriteria().andManufacturer_stateEqualTo(2));
        return platformExample;
    }

    public DrugTagExample selectDrugTagListByUpperClass(int class_code) {
        DrugTagExample drugTagExample=new DrugTagExample();
        drugTagExample.or(drugTagExample.createCriteria().andUpper_classEqualTo(class_code));
        return drugTagExample;
    }

    public DrugInfoExample AllNotClassifiedDrugsExample() {
        DrugInfoExample drugInfoExample=new DrugInfoExample();
        drugInfoExample.or(drugInfoExample.createCriteria().andClass_codeEqualTo(-1));
        return drugInfoExample;
    }

    public DrugTagExample AllDrugTagsLeafExample() {
        DrugTagExample drugTagExample=new DrugTagExample();
        drugTagExample.or(drugTagExample.createCriteria().andIs_leafEqualTo(true));
        return drugTagExample;
    }

    public UserssExample selectUserByOidAndAuthorityExample(Integer oid) {
        UserssExample userssExample=new UserssExample();
        userssExample.createCriteria().andOidEqualTo(oid).andIs_organization_acceptEqualTo(0).andUser_authorityNotEqualTo(1);
        return userssExample;
    }

    public UserssExample selectUserByOidAndAuthorityAndIs_organization_acceptExample(Integer oid) {
        UserssExample userssExample=new UserssExample();
        userssExample.createCriteria().andOidEqualTo(oid).andIs_organization_acceptEqualTo(1).andUser_authorityNotEqualTo(1);
        return userssExample;
    }

    public void updateCompanyDrugInfoTime(CompanyDrugInformation companyDrugInformation) {
        companyDrugInformation.setUpdate_date(getTime());
    }

    public UserssExample selectUserByUsernameExample(String username) {
        UserssExample userssExample=new UserssExample();
        userssExample.createCriteria().andUser_nameEqualTo(username);
        return userssExample;
    }

    public PlatformExample AllApplicablePlatformExample() {
        PlatformExample platformExample=new PlatformExample();
        platformExample.createCriteria().andOrole_typeNotEqualTo(0);
        return platformExample;
    }

    public CompanyDrugInformationExample selectCompanyDrugByDrug_id(int drug_id) {
        CompanyDrugInformationExample companyDrugInformationExample=new CompanyDrugInformationExample();
        companyDrugInformationExample.createCriteria().andDrug_idEqualTo(drug_id);
        return companyDrugInformationExample;
    }

    public void AuditCompanyDrugByAuditCode(List<CompanyDrugInformation> companyDrugInformations, int check_code) {
        if (check_code==1){
            for (CompanyDrugInformation companyDrugInformation:companyDrugInformations){
                companyDrugInformation.setState_of_drug("审核通过");
            }
        }
        else {
            for (CompanyDrugInformation companyDrugInformation:companyDrugInformations){
                companyDrugInformation.setState_of_drug("不通过");
            }
        }

    }

    public void unpassPlatform(Platform platform) {
        if(platform.getBuyer_state()==2)
            platform.setBuyer_state(3);
        if(platform.getSeller_state()==2)
            platform.setSeller_state(3);
        if(platform.getManufacturer_state()==2)
            platform.setManufacturer_state(3);
    }
}
